package com.budwk.app.jim.server;

import org.jim.core.packets.Command;
import org.jim.server.JimServer;
import org.jim.server.command.CommandManager;
import org.jim.server.command.handler.HandshakeReqHandler;
import org.jim.server.command.handler.LoginReqHandler;
import org.jim.server.config.ImServerConfig;
import org.jim.server.config.PropertyImServerConfigBuilder;
import org.nutz.ioc.Ioc;

/**
 * @author ZY.Wang
 * @since 2023/6/14
 */
public class ImServerStarter {

    public ImServerStarter(Ioc ioc) throws Exception {

        ImServerConfig imServerConfig = new PropertyImServerConfigBuilder("jim.properties").build();
        imServerConfig.setImUserListener(new ImUserListener());
        JimServer jimServer = new JimServer(imServerConfig);

        HandshakeReqHandler handshakeReqHandler = CommandManager.getCommand(Command.COMMAND_HANDSHAKE_REQ, HandshakeReqHandler.class);
        handshakeReqHandler.addMultiProtocolProcessor(new HandShakeProcessor());

        LoginReqHandler loginReqHandler = CommandManager.getCommand(Command.COMMAND_LOGIN_REQ, LoginReqHandler.class);
        loginReqHandler.setSingleProcessor(new LoginServiceProcessor());

        CommandManager.removeCommand(Command.COMMAND_CHAT_REQ);
        CommandManager.registerCommand(new JimChatReqHandler());
        JimChatReqHandler chatReqHandler = CommandManager.getCommand(Command.COMMAND_CHAT_REQ, JimChatReqHandler.class);
        chatReqHandler.setSingleProcessor(new ChatMessageProcessor(ioc));

        jimServer.start();
    }
}
